package com.walterweimod.auxiliary_attributes.mobeffect;

import com.google.common.base.CaseFormat;
import com.walterweimod.auxiliary_attributes.attribute.ModAttributes;
import com.walterweimod.auxiliary_attributes.capability.ModCapabilities;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeRangedValueBase;
import com.walterweimod.auxiliary_attributes.util.ModAttributeUtils;
import com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.UUID;

import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.getLogger;
import static com.walterweimod.auxiliary_attributes.util.ModAttributeUtils.getNameAttributeUUID;
import static com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils.getCapabilityValue;

public class BloodFeast extends MobEffect {
    public static final String DESCRIPTION = "blood_feast";

    public static final UUID BLOOD_FEAST_MAX_BLOOD_POOL_UUID = getNameAttributeUUID(
            ModAttributes.MAX_BLOOD_POOL.get(),
            DESCRIPTION
    );

    protected BloodFeast() {
        super(MobEffectCategory.BENEFICIAL, 0xFF00FF00);
    }

    @Override
    public @NotNull Component getDisplayName() {
        return new TextComponent(
                CaseFormat.LOWER_UNDERSCORE.to(
                        CaseFormat.UPPER_CAMEL, DESCRIPTION
                )
        );
    }

    @Override
    public void addAttributeModifiers(@NotNull LivingEntity p_19478_,
                                      @NotNull AttributeMap p_19479_,
                                      int  p_19480_) {

        ModAttributeUtils.addAttributeModifier(
                p_19478_,
                ModAttributes.MAX_BLOOD_POOL.get(),
                p_19480_,
                BLOOD_FEAST_MAX_BLOOD_POOL_UUID,
                AttributeModifier.Operation.ADDITION,
                false
        );

        ModCapabilityUtils.setCapabilityValue(
                p_19478_,
                ModCapabilities.BLOOD_POOL_VALUE,
                bloodPoolValue -> {
                    bloodPoolValue.setValue(0.0F);
                    bloodPoolValue.setLivingEntity(p_19478_);
                    bloodPoolValue.setAttributeMap(Map.of(
                            "max", ModAttributes.MAX_BLOOD_POOL.get()
                    ));
                }
        );

        if (!p_19478_.level.isClientSide()){
            getLogger().info("珠露初始化成功，战意值为：{}", getCapabilityValue(
                    p_19478_,
                    ModCapabilities.BLOOD_POOL_VALUE,
                    ChargeRangedValueBase::getValue,
                    0
            ));

            getLogger().info("珠露初始化成功，最大值为：{}", getCapabilityValue(
                    p_19478_,
                    ModCapabilities.BLOOD_POOL_VALUE,
                    ChargeRangedValueBase::getMaxValue,
                    0
            ));
        }

        super.addAttributeModifiers(p_19478_, p_19479_,  p_19480_);
    }

    @Override
    public void removeAttributeModifiers(@NotNull LivingEntity p_19469_,
                                         @NotNull AttributeMap p_19470_,
                                         int p_19471_) {
        ModAttributeUtils.removeAttributeModifier(
                p_19469_,
                ModAttributes.MAX_BLOOD_POOL.get(),
                BLOOD_FEAST_MAX_BLOOD_POOL_UUID
        );

        ModCapabilityUtils.setCapabilityValue(
                p_19469_,
                ModCapabilities.BLOOD_POOL_VALUE,
                bloodPoolValue -> bloodPoolValue.setValue(0.0F)
        );

        if (!p_19469_.level.isClientSide()){
            getLogger().info("珠露移除成功");
        }

        super.removeAttributeModifiers(p_19469_, p_19470_, p_19471_);
    }
}
